package com.anan.controller;

import com.anan.constants.Constants;
import com.anan.pojo.dto.AddRemarkDTO;
import com.anan.pojo.dto.QryRemarkDTO;
import com.anan.pojo.vo.RemarkOfOpusAndProdVO;
import com.anan.service.RemarkService;
import com.anan.utils.ComResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author anan
 * @date 2023/05/10/12:04
 **/
@RestController
@Slf4j
@RequestMapping("/salvage/remark")
public class RemarkController {

    @Resource
    private RemarkService remarkService;

    /**
     * 评论作品或商品
     */
    @PostMapping("/addOneRemark")
    public ComResult addOneRemark(@RequestBody AddRemarkDTO addRemarkDTO) {
        ComResult comResult = new ComResult();
        comResult.setCode(Constants.RET_CODE.FAILED);

        if (null == addRemarkDTO.getRemark()) {
            comResult.setMessage("请输入评论！");
            return comResult;
        }


        if(null == addRemarkDTO.getStudentId()) {
            log.info("===== 未传入用户的id =====");
            comResult.setMessage("未传入用户的id");//TODO:delete
            return comResult;
        }

        boolean flag = remarkService.addOneRemark(addRemarkDTO);
        if(flag) {
            comResult.setCode(Constants.RET_CODE.SUCCESS);
            comResult.setMessage("评论成功！");
        } else {
            comResult.setMessage("评论失败！");
        }
        return comResult;
    }

    /**
     * 查询评论
     */
    @PostMapping("/getRemarks")
    public ComResult getRemarks(@RequestBody QryRemarkDTO qryRemarkDTO) {
        ComResult comResult = new ComResult();
        comResult.setCode(Constants.RET_CODE.FAILED);

//        if (null == qryRemarkDTO.getStudentId()) {
//            log.info("===== 用户id未传 =====");
//            comResult.setMessage("用户id未传");
//            return comResult;
//        }
        if (null == qryRemarkDTO.getOpusId() && null == qryRemarkDTO.getProductId()) {
            log.info("===== 帖子或商品id未传 =====");
            comResult.setMessage("帖子或商品id未传");
            return comResult;
        }

        RemarkOfOpusAndProdVO remarkVO = remarkService.getRemarkOfOpus(qryRemarkDTO);
        if (null != remarkVO) {
            comResult.setData(remarkVO);
            comResult.setCode(Constants.RET_CODE.SUCCESS);
            comResult.setMessage("查询成功！");
        } else {
            comResult.setMessage("查询失败！");
        }

        return comResult;
    }
}
